home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / lib / rcscripts / awk / fixlafiles.awk < prev    next >
Text File  |  2005-10-13  |  8KB  |  315 lines

  1. # Copyright 1999-2005 Gentoo Foundation
  2. # Distributed under the terms of the GNU General Public License v2
  3. # $Header: /var/cvsroot/gentoo-x86/sys-devel/gcc/files/awk/fixlafiles.awk,v 1.14 2005/09/24 07:31:28 vapier Exp $
  4.  
  5. #
  6. # Helper functions
  7. #
  8. function printn(string) {
  9.     system("echo -n \"" string "\"")
  10. }
  11. function einfo(string) {
  12.     system("echo -e \" \\e[32;01m*\\e[0m " string "\"")
  13. }
  14. function einfon(string) {
  15.     system("echo -ne \" \\e[32;01m*\\e[0m " string "\"")
  16. }
  17. function ewarn(string) {
  18.     system("echo -e \" \\e[33;01m*\\e[0m " string "\"")
  19. }
  20. function ewarnn(string) {
  21.     system("echo -ne \" \\e[33;01m*\\e[0m " string "\"")
  22. }
  23. function eerror(string) {
  24.     system("echo -e \" \\e[31;01m*\\e[0m " string "\"")
  25. }
  26.  
  27. #
  28. # assert(condition, errmsg)
  29. #   assert that a condition is true.  Otherwise exit.
  30. #
  31. function assert(condition, string) {
  32.     if (! condition) {
  33.         printf("%s:%d: assertion failed: %s\n",
  34.                FILENAME, FNR, string) > "/dev/stderr"
  35.         _assert_exit = 1
  36.         exit 1
  37.     }
  38. }
  39.  
  40. #
  41. # system(command, return)
  42. #   wrapper that normalizes return codes ...
  43. #
  44. function dosystem(command, ret) {
  45.     ret = 0
  46.     ret = system(command)
  47.     if (ret == 0)
  48.         return 1
  49.     else
  50.         return 0
  51. }
  52.  
  53. BEGIN {
  54.     #
  55.     # Get our variables from environment
  56.     #
  57.     OLDVER = ENVIRON["OLDVER"]
  58.     OLDCHOST = ENVIRON["OLDCHOST"]
  59.  
  60.     if (OLDVER == "") {
  61.         eerror("Could not get OLDVER!");
  62.         exit 1
  63.     }
  64.  
  65.     # Setup some sane defaults
  66.     LIBCOUNT = 2
  67.     HAVE_GCC34 = 0
  68.     DIRLIST[1] = "/lib"
  69.     DIRLIST[2] = "/usr/lib"
  70.  
  71.     #
  72.     # Walk /etc/ld.so.conf to discover all our library paths
  73.     #
  74.     pipe = "cat /etc/ld.so.conf | sort 2>/dev/null"
  75.     while(((pipe) | getline ldsoconf_data) > 0) {
  76.         if (ldsoconf_data !~ /^[[:space:]]*#/) {
  77.             if (ldsoconf_data == "") continue
  78.  
  79.             # Remove any trailing comments
  80.             sub(/#.*$/, "", ldsoconf_data)
  81.             # Remove any trailing spaces
  82.             sub(/[[:space:]]+$/, "", ldsoconf_data)
  83.  
  84.             # If there's more than one path per line, split 
  85.             # it up as if they were sep lines
  86.             split(ldsoconf_data, nodes, /[:,[:space:]]/)
  87.  
  88.             # Now add the rest from ld.so.conf
  89.             for (x in nodes) {
  90.                 # wtf does this line do ?
  91.                 sub(/=.*/, "", nodes[x])
  92.                 # Prune trailing /
  93.                 sub(/\/$/, "", nodes[x])
  94.  
  95.                 if (nodes[x] == "") continue
  96.  
  97.                 #
  98.                 # Drop the directory if its a child directory of
  99.                 # one that was already added ...
  100.                 # For example, if we have:
  101.                 #   /usr/lib /usr/libexec /usr/lib/mozilla /usr/lib/nss
  102.                 # We really just want to save /usr/lib /usr/libexec
  103.                 #
  104.                 CHILD = 0
  105.                 for (y in DIRLIST) {
  106.                     if (nodes[x] ~ "^" DIRLIST[y] "(/|$)") {
  107.                         CHILD = 1
  108.                         break
  109.                     }
  110.                 }
  111.                 if (CHILD) continue
  112.  
  113.                 DIRLIST[++LIBCOUNT] = nodes[x]
  114.             }
  115.         }
  116.     }
  117.     close(pipe)
  118.  
  119.     #
  120.     # Get line from gcc's output containing CHOST
  121.     #
  122.     pipe = "gcc -print-file-name=libgcc.a 2>/dev/null"
  123.     if ((!((pipe) | getline TMP_CHOST)) || (TMP_CHOST == "")) {
  124.         close(pipe)
  125.  
  126.         # If we fail to get the CHOST, see if we can get the CHOST
  127.         # portage thinks we are using ...
  128.         pipe = "/usr/bin/portageq envvar 'CHOST'"
  129.         assert(((pipe) | getline CHOST), "(" pipe ") | getline CHOST")
  130.     } else {
  131.         # Check pre gcc-3.4.x versions
  132.         CHOST = gensub("^.+lib/gcc-lib/([^/]+)/[0-9]+.+$", "\\1", 1, TMP_CHOST)
  133.  
  134.         if (CHOST == TMP_CHOST || CHOST == "") {
  135.             # Check gcc-3.4.x or later
  136.             CHOST = gensub("^.+lib/gcc/([^/]+)/[0-9]+.+$", "\\1", 1, TMP_CHOST);
  137.  
  138.             if (CHOST == TMP_CHOST || CHOST == "")
  139.                 CHOST = ""
  140.             else
  141.                 HAVE_GCC34 = 1
  142.         }
  143.     }
  144.     close(pipe)
  145.  
  146.     if (CHOST == "") {
  147.         eerror("Could not get gcc's CHOST!")
  148.         exit 1
  149.     }
  150.  
  151.     if (OLDCHOST != "")
  152.         if (OLDCHOST == CHOST)
  153.             OLDCHOST = ""
  154.  
  155.     GCCLIBPREFIX_OLD = "/usr/lib/gcc-lib/"
  156.     GCCLIBPREFIX_NEW = "/usr/lib/gcc/"
  157.  
  158.     if (HAVE_GCC34)
  159.         GCCLIBPREFIX = GCCLIBPREFIX_NEW
  160.     else
  161.         GCCLIBPREFIX = GCCLIBPREFIX_OLD
  162.  
  163.     GCCLIB = GCCLIBPREFIX CHOST
  164.  
  165.     if (OLDCHOST != "") {
  166.         OLDGCCLIB1 = GCCLIBPREFIX_OLD OLDCHOST
  167.         OLDGCCLIB2 = GCCLIBPREFIX_NEW OLDCHOST
  168.     }
  169.  
  170.     # Get current gcc's version
  171.     pipe = "gcc -dumpversion"
  172.     assert(((pipe) | getline NEWVER), "(" pipe ") | getline NEWVER)")
  173.     close(pipe)
  174.  
  175.     if (NEWVER == "") {
  176.         eerror("Could not get gcc's version!")
  177.         exit 1
  178.     }
  179.  
  180.     # Nothing to do ?
  181.     if ((OLDVER == NEWVER) && (OLDCHOST == ""))
  182.         exit 0
  183.  
  184.     #
  185.     # Ok, now let's scan for the .la files and actually fix them up
  186.     #
  187.     for (x = 1; x <= LIBCOUNT; x++) {
  188.         # Do nothing if the target dir is gcc's internal library path
  189.         if (DIRLIST[x] ~ GCCLIBPREFIX_OLD ||
  190.             DIRLIST[x] ~ GCCLIBPREFIX_NEW)
  191.             continue
  192.  
  193.         einfo("  [" x "/" LIBCOUNT "] Scanning " DIRLIST[x] " ...")
  194.  
  195.         pipe = "find " DIRLIST[x] "/ -name '*.la' 2>/dev/null"
  196.         while (((pipe) | getline la_files) > 0) {
  197.  
  198.             # Do nothing if the .la file is located in gcc's internal lib path
  199.             if (la_files ~ GCCLIBPREFIX_OLD ||
  200.                 la_files ~ GCCLIBPREFIX_NEW)
  201.                 continue
  202.  
  203.             CHANGED = 0
  204.             CHOST_CHANGED = 0
  205.  
  206.             # See if we need to fix the .la file
  207.             while ((getline la_data < (la_files)) > 0) {
  208.                 if (OLDCHOST != "") {
  209.                     if ((gsub(OLDGCCLIB1 "[/[:space:]]+",
  210.                               GCCLIB, la_data) > 0) ||
  211.                         (gsub(OLDGCCLIB2 "[/[:space:]]+",
  212.                               GCCLIB, la_data) > 0)) {
  213.                         CHANGED = 1
  214.                         CHOST_CHANGED = 1
  215.                     }
  216.                 }
  217.                 if (OLDVER != NEWVER) {
  218.                     if ((gsub(GCCLIBPREFIX_OLD CHOST "/" OLDVER "[/[:space:]]*",
  219.                               GCCLIB "/" NEWVER, la_data) > 0) ||
  220.                         (gsub(GCCLIBPREFIX_NEW CHOST "/" OLDVER "[/[:space:]]*",
  221.                               GCCLIB "/" NEWVER, la_data) > 0))
  222.                         CHANGED = 1
  223.                 }
  224.             }
  225.             close(la_files)
  226.  
  227.             # Do the actual changes in a second loop, as we can then
  228.             # verify that CHOST_CHANGED among things is correct ...
  229.             if (CHANGED) {
  230.                 ewarnn("    FIXING: " la_files " ...")
  231.  
  232.                 if (CHANGED)
  233.                     printn("[")
  234.  
  235.                 # Clear the temp file (removing rather than '>foo' is better
  236.                 # out of a security point of view?)
  237.                 dosystem("rm -f " la_files ".new")
  238.  
  239.                 while ((getline la_data < (la_files)) > 0) {
  240.                     if (OLDCHOST != "") {
  241.                         tmpstr = gensub(OLDGCCLIB1 "([/[:space:]]+)",
  242.                                         GCCLIB "\\1", "g", la_data)
  243.                         tmpstr = gensub(OLDGCCLIB2 "([/[:space:]]+)",
  244.                                         GCCLIB "\\1", "g", tmpstr)
  245.  
  246.                         if (la_data != tmpstr) {
  247.                             printn("c")
  248.                             la_data = tmpstr
  249.                         }
  250.  
  251.                         if (CHOST_CHANGED > 0) {
  252.                             # We try to be careful about CHOST changes outside
  253.                             # the gcc library path (meaning we cannot match it
  254.                             # via /GCCLIBPREFIX CHOST/) ...
  255.  
  256.                             # Catch:
  257.                             #
  258.                             #  dependency_libs=' -L/usr/CHOST/{bin,lib}'
  259.                             #
  260.                             gsub("-L/usr/" OLDCHOST "/",
  261.                                  "-L/usr/" CHOST "/", la_data)
  262.                             # Catch:
  263.                             #
  264.                             #  dependency_libs=' -L/usr/lib/gcc-lib/CHOST/VER/../../../../CHOST/lib'
  265.                             #
  266.                             la_data = gensub("(" GCCLIB "/[^[:space:]]+)/" OLDCHOST "/",
  267.                                              "\\1/" CHOST "/", "g", la_data)
  268.                         }
  269.                     }
  270.  
  271.                     if (OLDVER != NEWVER) {
  272.                         # Catch:
  273.                         #
  274.                         #  dependency_libs=' -L/usr/lib/gcc/CHOST/VER'
  275.                         #
  276.                         tmpstr = gensub(GCCLIBPREFIX_OLD CHOST "/" OLDVER "([/[:space:]]+)",
  277.                                         GCCLIB "/" NEWVER "\\1", "g", la_data)
  278.                         tmpstr = gensub(GCCLIBPREFIX_NEW CHOST "/" OLDVER "([/[:space:]]+)",
  279.                                         GCCLIB "/" NEWVER "\\1", "g", tmpstr)
  280.  
  281.                         if (la_data != tmpstr) {
  282.                             # Catch:
  283.                             #
  284.                             #  dependency_libs=' -L/usr/lib/gcc-lib/../../CHOST/lib'
  285.                             #
  286.                             # in cases where we have gcc34
  287.                             tmpstr = gensub(GCCLIBPREFIX_OLD "(../../" CHOST "/lib)",
  288.                                             GCCLIBPREFIX "\\1", "g", tmpstr)
  289.                             tmpstr = gensub(GCCLIBPREFIX_NEW "(../../" CHOST "/lib)",
  290.                                             GCCLIBPREFIX "\\1", "g", tmpstr)
  291.                             printn("v")
  292.                             la_data = tmpstr
  293.                         }
  294.                     }
  295.  
  296.                     print la_data >> (la_files ".new")
  297.                 }
  298.  
  299.                 if (CHANGED)
  300.                     print "]"
  301.  
  302.                 close(la_files)
  303.                 close(la_files ".new")
  304.  
  305.                 assert(dosystem("mv -f " la_files ".new " la_files),
  306.                        "dosystem(\"mv -f " la_files ".new " la_files "\")")
  307.             }
  308.         }
  309.  
  310.         close(pipe)
  311.     }
  312. }
  313.  
  314. # vim:ts=4
  315.